


import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;


public class StorageServer {

    private final String host = "localhost";
    private final int mport = 55555;

    private transient ServerSocket ss;
    private ConcurrentLinkedQueue<ReplyTo> orders;
    StorageServerT svt;
    int port, capacity;
    private transient ObjectOutputStream out;
    Socket sock;

    public StorageServer(int port, int capacity) throws Exception{
        this.port = port;
        this.capacity = capacity;
        this.orders = new ConcurrentLinkedQueue<ReplyTo> ();


        ss = new ServerSocket(port);
        ss.setReuseAddress(true);
        System.out.println("StorageServer up!");

        
        //Envia informação ao CentralServer
        sock = new Socket(host, mport);
        this.out= new ObjectOutputStream(sock.getOutputStream());
        Address info = new Address(port, host);
        out.writeObject(info);
        out.close();

        
        //Cria um StorageServerQ que vai executando os Request da Queue
        new Thread(new StorageServerQ(orders)).start();

        
        //Ciclo que aceita ligações dos clientes
        while(true){
            Socket client= ss.accept();
            System.out.println("Request accepted!");
            new Thread(new StorageServerT(client, orders)).start();
        }
    }

    public ConcurrentLinkedQueue<ReplyTo> getOrders() {
        return orders;
    }

    public void setOrders(ConcurrentLinkedQueue<ReplyTo> orders) {
        this.orders = orders;
    }

    public ObjectOutputStream getOut() {
        return out;
    }

    public void setOut(ObjectOutputStream out) {
        this.out = out;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Socket getSock() {
        return sock;
    }

    public void setSock(Socket sock) {
        this.sock = sock;
    }



    



    public static void main(String[] args){
        try {
            StorageServer storage = new StorageServer(55558,20000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
